package mainBoerse;
import java.util.ArrayList;

import stockPrice.StockPriceProvider;
import exceptions.NotEnoughSharesToSell;


public class ShareItem extends Asset implements Comparable<ShareItem> {
	
	

	private int size;
	private float last_course;	
	
	public ShareItem(String name) {
		
		this.size = 0;
		this.name = name;
		this.value = 0;
		this.last_course = 1;
		
	}
	
	public void reduceShares(int count) throws NotEnoughSharesToSell {
		if(this.size < count) throw new NotEnoughSharesToSell("not enough shares to sell");
		else this.size -= count;
	}

	

	public int getSize() {
		return this.size;
	}
	
	@Override
	public float getValue() {
		float course = 0;
		ArrayList<Share> share = StockPriceProvider.getSHARES();
		for(int i = 0; i < share.size(); i++) {
			if(share.get(i).getAktienName().equals(this.name)) course = share.get(i).getAktienKurs();
		}
		this.last_course = course;
		return this.size * course;
		
	}
	
	public void addShares(int count, float course) {
		
		this.size += count;
		this.last_course = course;
		this.value = this.last_course * this.size;
	}
	
	
	public void buyShare(int count, long course) {
		
		this.size += count;
		this.last_course = course;
		this.value = this.last_course * this.size;
		
	}
	
	public String toString() {
		
		StringBuilder builder = new StringBuilder();
		
			//if(this.shares[i] != null) {
				builder.append("Name: ");				
				builder.append(this.name + "<br>");
				/*builder.append(" Letzter Kurs: ");				
				builder.append(this.last_course + "<br>");*/
				builder.append(" Anzahl: ");
				builder.append(this.size + "<br>");
				//builder.append(System.getProperty("line.separator"));
			//}		
		
		return builder.toString();
	}

	@Override
	public int compareTo(ShareItem o) {
		
		return this.name.compareTo(o.name);
	}
}
